module scene {

    export class StartScene extends eui.Component {

        public static readonly SHOW_FRIEND_RANK: string = "show_friend_rank";

        public static readonly PLAY_GAME_EVENT: string = "start_game";

        public rootGroup: eui.Group;

        public btnMore: eui.Image;

        public btnRank: eui.Image;

        public btnMusicOn: eui.Image;

        public btnMusicOff: eui.Image;

        public btnGroup: eui.Group;

        private pushBtnMusicOn: ui.PushButton;

        private pushBtnMusicOff: ui.PushButton;

        private curAdIdx: number = 0;

        private guideGroup: eui.Group;

        private adGroup: eui.Group;

        private start_bg_page: eui.Image;

        public btnRedPacket: eui.Image;

        private buttonOpenRedPakcet: ui.PushButton;

        public starGameBtn: eui.Image;

        public starBtnMov: egret.tween.TweenGroup;

        private startBtnMovie: eui.Image;

        private maskImg: eui.Image;

        private meteorMovie: egret.tween.TweenGroup
        private meteorMovie2: egret.tween.TweenGroup

        private addToMyMovie: egret.tween.TweenGroup
        private adList = [];
        public list: egret.DisplayObjectContainer;
        private count = 0;
        private ad_imgs = [];


        private timer1
        private timer2
        private timer3

        constructor() {
            super();
            this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
            this.skinName = "resource/assets/GameRes/game/skins/StartPageSkin.exml";
        }

        public show(): void {
            this.visible = true;
        }

        private onComplete(): void {

            manager.StatisticsManager.sceneCount(1003)

            let adManager = manager.GameManager.getInstance().getAdManager();
            console.log(1)
            this.adList = adManager.drawer_ad || [];


            let btn: ui.PushButton = new ui.PushButton(this.btnRank);
            btn.addEventListener(ui.PushButton.CLICK, this.onRank, this);
            this.btnGroup.addChild(btn);

            // btn = new ui.PushButton(this.btnMore);
            // btn.addEventListener(ui.PushButton.CLICK, this.onMore, this);
            // this.btnGroup.addChild(btn);

            this.pushBtnMusicOn = new ui.PushButton(this.btnMusicOn);
            this.pushBtnMusicOn.addEventListener(ui.PushButton.CLICK, this.onMusicOn, this);
            this.btnGroup.addChild(this.pushBtnMusicOn);

            this.start_bg_page.width = manager.DisplayManager.STAGE_WIDTH;
            this.start_bg_page.height = manager.DisplayManager.STAGE_HEIGHT;

            this.startBtnMovie.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onPlay, this);

            this.startBtnMovie.mask = this.maskImg

            this.starBtnMov.play(0);     
            this.addToMyMovie.play(0)
                      
            this.timer1 = setInterval(() => {
                this.starBtnMov.play(0);  
            }, 3000)
            this.timer1 = setInterval(() => {
                let random = Math.random() 
                if(random <= 0.4 ){
                    this.meteorMovie.play(0);  
                }else if(random >= 0.6 ){
                    this.meteorMovie2.play(0);  
                }else{
                    this.meteorMovie.play(0);
                    this.meteorMovie2.play(0);
                }
            }, 3500)
            this.timer1 = setInterval(() => {
                this.addToMyMovie.play(0)
            }, 4000)


            if (this.adList.length) {
                for (let i = 0; i < this.adList.length; i++) {
                    this.createAd(i);
                }
            }
            

        }

        protected createChildren() {
            super.createChildren();
            let btn: ui.PushButton;
        }

        public update(): void {

        }

        private onRank(eve: egret.Event): void {
            let event: events.SceneEvent = new events.SceneEvent(game.SceneIds.RANK_SCENE_ID, {}, events.SceneEvent.POPUP_SHOW_TYPE);
            facade.GameFacade.getInstance().dispatchEvent(event);
        }

        public resize(): void {

        }

        private onMusicOn(eve: egret.Event): void {
            manager.GameManager.getInstance().getSetting().musicToggle();
        }

        private onMusicOff(eve: egret.Event): void {
            manager.GameManager.getInstance().getSetting().musicToggle();
        }

        private onPlay(eve: egret.Event): void {
            manager.StatisticsManager.click(147)

            let event: events.SceneEvent = new events.SceneEvent(game.SceneIds.GAMEING_SCENE_ID);
            facade.GameFacade.getInstance().dispatchEvent(event);
        }




        // private loadMoreImg(): void {
        //     if (!manager.GameManager.isProduct) {
        //         return;
        //     }
        //     manager.GameManager.getInstance().adManager.more_game_ad;
        //     let that = this;
        //     let moreGameAd: manager.Ad = manager.GameManager.getInstance().adManager.more_game_ad;
        //     this.curAdIdx = (this.curAdIdx + 1) % moreGameAd.poster.length;
        //     if (moreGameAd.poster.length > 0) {
        //         let poster: manager.Poster = moreGameAd.poster[this.curAdIdx];
        //         let bgImgUrl: string = poster.icon;
        //         let imgLoader = new egret.ImageLoader();
        //         imgLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
        //             let imageLoader = <egret.ImageLoader>event.currentTarget;
        //             let texture = new egret.Texture();
        //             texture.bitmapData = imageLoader.data;
        //             that.btnMore.texture = texture;
        //         }, this);
        //         imgLoader.load(bgImgUrl);
        //     }
        // }

        private createAd(index) {
            let bgImgUrl: string = this.adList[index].img;
            let imgLoader = new egret.ImageLoader();
            imgLoader.addEventListener(egret.Event.COMPLETE, this.loadAd.bind(this, index), this);
            imgLoader.load(bgImgUrl);
        }

        private loadAd(index, event: egret.Event) {
            let imageLoader = <egret.ImageLoader>event.currentTarget;
            let texture = new egret.Texture();
            texture._setBitmapData(imageLoader.data);
            let bitmap = new egret.Bitmap();
            bitmap.$setTexture(texture);
            this.ad_imgs[index] = bitmap;
            this.count++;
            if (this.count >= this.adList.length) {
                this.count = 0;
                this.drawList(index);
            }
        }

        private drawList(index){
            let w = 95, h = 95, w2 = 95, gap=10;
            let list = new egret.DisplayObjectContainer();
            var shp_out = new egret.Shape();
            var shp_inner = new egret.Shape();
            list.addChild( shp_out );
            list.addChild( shp_inner );
            for (var i = 0; i < 4 ; i++) {
                var bitmap = this.ad_imgs[i + 1];
                var container = new egret.DisplayObjectContainer();
                container.touchEnabled = true;
                container.width = w;
                container.height = h + 20;
                let l = 0
                let n = i
                if(i >= 4){
                    n = n - 4
                    l = 1
                }
                container.x = n * (w + 15) 
                container.y = l * (h + 15)

                bitmap.width = w2;
                bitmap.height = w2;
                bitmap.x = bitmap.y = (w - w2) * 0.5;
                container.addChild( bitmap );
                var label:egret.TextField = new egret.TextField(); 
                label.verticalAlign = "top";
                label.width = w;
                label.height = 20;
                label.y = w + 5;
                label.textAlign = "center";
                label.textColor = 0xffffff;
                label.size = 18;
                label.text = this.adList[i + 1].title; 
                container.addChild( label );
                list.addChild(container);
                container.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickAd.bind(this,i), this);
            }
            list.x = (this.adGroup.width - list.width) / 2;
            list.y = (this.adGroup.height - list.height) / 2;
            this.adGroup.addChild(list);
        }

        private clickAd(index, eve) {
            eve.stopImmediatePropagation();
            let ad = this.adList[index];
            manager.GameManager.getInstance().getAdManager().showAd("drawer_ad", ad);
        }
    }
}